package com.wanshi.servlet;

import com.wanshi.entity.Product;
import com.wanshi.entity.User;
import com.wanshi.service.CartService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebServlet("/cart/cartList")
public class cartList extends HttpServlet {

    CartService cartService = new CartService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //查询当前用户购物车购买的所有商品 及 数量  根据用户id
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        Map<Product,Integer> map = cartService.findCartByUserId(user.getId()+"");

        //存入request域,请求转发到jsp
        request.setAttribute("map",map);
        request.getRequestDispatcher("/cart/cart.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

